<template>

  <div>
    <el-row>
      <el-col :span="24">
        <el-form :inline="true">
          <el-form-item label="请输入用户昵称:">
            <el-input v-model="param.userNickName" placeholder="请输入用户昵称" @change="findOrderByCondition">
            </el-input>
          </el-form-item>
          <el-form-item label="请选择下单时间:">
            <el-date-picker v-model="orderDateRange" type="datetimerange" range-separator="至" start-placeholder="开始日期"
              end-placeholder="结束日期" format="yyyy-MM-ddTHH:mm:ss" @change="findOrderByCondition">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="请选择支付时间:">
            <el-date-picker v-model="payDateRange" type="datetimerange" range-separator="至" start-placeholder="开始日期"
              end-placeholder="结束日期" format="yyyy-MM-ddTHH:mm:ss" @change="findOrderByCondition">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="请选择订单状态:">
            <el-select v-model="param.orderProductState" placeholder="请选择" clearable @change="findOrderByCondition">
              <el-option :value="0" label="待支付"></el-option>
              <el-option :value="1" label="已支付"></el-option>
              <el-option :value="2" label="待收货"></el-option>
              <el-option :value="3" label="收货确认"></el-option>
              <el-option :value="4" label="售后生效中"></el-option>
              <el-option :value="5" label="订单完成"></el-option>
              <el-option :value="6" label="取消"></el-option>
              <el-option :value="7" label="退款中"></el-option>
              <el-option :value="8" label="退款成功"></el-option>

            </el-select>
          </el-form-item>

          <el-form-item>
            <el-badge :value="orderRefundSize">
              <el-button size="small" @click="showRefundDialog">退款申请</el-button>
            </el-badge>
          </el-form-item>
        </el-form>

      </el-col>
    </el-row>

    <el-row style="margin-top: 20px;">
      <el-col :span="24">
        <el-table :data="page.records" style="width: 100%">
          <el-table-column prop="orderProductNum" label="订单编号" width="100" align="center" label-align="center">
          </el-table-column>
          <el-table-column prop="orderPrice" label="总价" width="50" align="center" label-align="center">
          </el-table-column>
          <el-table-column prop="userInfoDto.userNickName" label="用户昵称" width="100" align="center" label-align="center">
          </el-table-column>
          <el-table-column prop="userInfoDto.userPhone" label="用户电话" width="110" align="center" label-align="center">
          </el-table-column>
          <el-table-column prop="addressInfoDto.recevierName" label="收货人姓名" width="100" align="center"
            label-align="center">
          </el-table-column>
          <el-table-column prop="addressInfoDto.recevierPhone" label="收货人电话" width="110" align="center"
            label-align="center">
          </el-table-column>
          <el-table-column prop="addressInfoDto.addressName" label="地址详情" width="180" align="center"
            label-align="center">
          </el-table-column>
          <el-table-column prop="orderTime" label="下单时间" width="180" align="center" label-align="center">
          </el-table-column>
          <el-table-column prop="payTime" label="支付时间" width="180" align="center" label-align="center">
          </el-table-column>
          <el-table-column prop="orderPrice" label="订单总价" width="180" align="center" label-align="center">
          </el-table-column>
          <el-table-column prop="orderScore" label="订单评分" width="180" align="center" label-align="center">
          </el-table-column>
          <el-table-column label="评论状态" width="180" align="center" label-align="center">
            <template slot-scope="scope">{{ getReviewStateText(scope.row.reviewState)}}</template>
          </el-table-column>
          <el-table-column label="订单状态" width="180" align="center" label-align="center">
            <template slot-scope="scope">{{ getOrderStateText(scope.row.orderProductState)}}</template>
          </el-table-column>
          <el-table-column label="商品清单" width="300" align="center" label-align="center">
            <template slot-scope="scope">
              <div v-for="(productInfo, index) in scope.row.productInfos" :key="index">
                <div>药品名称：{{ productInfo.pInfoName }}</div>
                <div>单价：{{ productInfo.pInfoPrice }}</div>
                <div>购买数量：{{ productInfo.number }}</div>
              </div>
            </template>
          </el-table-column>




        </el-table>

        <div>
          <!--分页-->
          <el-pagination @size-change=" handleSizeChange" @current-change="handleCurrentChange"
            :current-page="param.pageNum" :page-sizes="[5, 8, 10]" :page-size="param.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="page.total">
          </el-pagination>
        </div>

        <el-dialog :visible.sync="refundDialogVisible" title="退款申请" width="90%">
          <el-table :data="orderRefund" style="width: 100%">
            <el-table-column prop="orderProductNum" label="商品订单编号" width="100" align="center" label-align="center">
            </el-table-column>
            <el-table-column prop="orderPrice" label="商品总价" width="100" align="center" label-align="center">
            </el-table-column>
            <el-table-column prop="refundReason" label="退款原因" width="180" align="center" label-align="center">
            </el-table-column>
            <el-table-column prop="refundTime" label="申请时间" width="180" align="center" label-align="center">
            </el-table-column>
            <el-table-column prop="dealTime" label="处理时间" width="180" align="center" label-align="center">
            </el-table-column>
            <el-table-column label="操作" width="200" align="center" label-align="center">
              <template slot-scope="scope">
                <el-button type="text" @click="okRefund(scope.row)">同意</el-button>
                <el-button type="text" @click="showNotRefund(scope.row)">驳回</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-dialog>
        <el-dialog :visible.sync="notRefundVisible" title="驳回申请" width="60%">
          <el-form :model="updateRefundParam" label-width="260px">
            <el-form-item label="驳回理由：">
              <el-input v-model="updateRefundParam.reason" placeholder="请输入驳回理由" clearable></el-input>
            </el-form-item>
          </el-form>
          <el-button type="primary" @click="notRefund()">确定</el-button>
          <el-button type="primary" @click="notRefundVisible=false">取消</el-button>
        </el-dialog>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  export default {
    name: "Order",
    data() {
      return {
        param: {
          pageNum: 1,
          pageSize: 5,
          orderProductState: "", //订单状态码
          orderMinTime: "", //在时间之后下单
          orderMaxTime: "", //在时间之前下单
          payMinTime: "", //在时间之后支付
          payMaxTime: "", //在时间之前支付
          userNickName: "", //用户昵称
        },
        orderDateRange: [], // 用于绑定下单日期选择器的数组
        payDateRange: [], // 用于绑定支付日期选择器的数组
        page: {},
        orderRefund: [], //订单退款申请集合
        refundDialogVisible: false, //退款申请窗口
        notRefundVisible: false, //驳回申请窗口
        updateRefundParam: {
          orderProductNum: "",
          reason: "",
        },
        headers: {},
        orderRefundSize:null,
      }

    },
    methods: {
      //展示驳回窗口
      showNotRefund(row) {
        this.updateRefundParam = JSON.parse(JSON.stringify(row))
        this.notRefundVisible = true
      },
      //驳回退款申请
      notRefund() {
        this.$http.post("/order/orderRefund/disagreeRefund.api", this.updateRefundParam).then(resp => {
          if (resp.data.code == 200) {
            this.notRefundVisible = false
            this.SuccessTip(resp.data)
            this.loadAllRefund();
            this.loadData();
          } else {
            this.ErrorTip(resp.data)
          }
        })
      },
      //同意退款申请
      okRefund(row) {
        this.updateRefundParam = JSON.parse(JSON.stringify(row))
        this.$http.post("/order/orderRefund/agreeRefund.api", this.updateRefundParam).then(resp => {
          if (resp.data.code == 200) {
            this.SuccessTip(resp.data)
            this.loadAllRefund();
            this.loadData();
          } else {
            this.ErrorTip(resp.data)
          }
        })
      },
      showRefundDialog() {
        // 显示退款弹窗
        this.refundDialogVisible = true;
      },
      //订单状态
      getOrderStateText(orderProductState) {
        switch (orderProductState) {
          case 0:
            return '待支付';
          case 1:
            return '已支付';
          case 2:
            return '待收货';
          case 3:
            return '收货确认';
          case 4:
            return '售后生效中';
          case 5:
            return '订单完成';
          case 6:
            return '取消';
          case 7:
            return '退款中';
          case 8:
            return '退款成功';
          case 9:
            return '删除';
          default:
            return '未知状态';
        }
      },
      //评论状态
      getReviewStateText(reviewState) {
        switch (reviewState) {
          case 0:
            return '未评价';
          case 1:
            return '已评价';
          default:
            return '未知状态';
        }
      },
      //查询所有订单
      loadData() {
        this.$http.post("/order/orderProduct/queryOrderProductsByCondition.api", this.param).then(resp => {
          this.page = resp.data.data
        })
      },
      //查询所有退款
      loadAllRefund() {
        this.$http.get("/order/orderRefund/queryAllRefund.api").then(resp => {
          this.orderRefund = resp.data.data
          if(this.orderRefund.length==0){
            this.orderRefundSize=null
          }else{
            this.orderRefundSize=this.orderRefund.length
          }
        })
      },
      //通过信息查询
      findOrderByCondition() {
        this.param.orderMinTime = this.orderDateRange[0];
        this.param.orderMaxTime = this.orderDateRange[1];
        this.param.payMinTime = this.payDateRange[0];
        this.param.payMaxTime = this.payDateRange[1];
        this.param.pageNum = 1;
        this.loadData();
      },
      handleSizeChange(pageSize) {
        this.param.pageSize = pageSize;
        this.loadData();
      },
      handleCurrentChange(pageNum) {
        this.param.pageNum = pageNum;
        this.loadData();
      },
      SuccessTip(data) { //成功
        this.$message({
          showClose: true,
          message: data.msg,
          type: "success"
        });
      },
      ErrorTip(data) { //失败
        this.$message({
          showClose: true,
          message: data.msg,
          type: "error"
        });
      },

    },
    created() {
      this.loadData();
      this.loadAllRefund();
      this.headers["token"] = localStorage.getItem("token");
    }
  }
</script>

<style>
</style>
